package spring.angularjs.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.angularjs.domain.Account;
import spring.angularjs.domain.AccountType;
import spring.angularjs.domain.Customer;
import spring.angularjs.domain.Statusaccount;
import spring.angularjs.domain.json.AccountJson;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value = "/account")
public class AccountController {

	@Autowired
	private Services services;

	@RequestMapping(value = "start")
	public String goAccountPage() {
		return "account";
	}

	@RequestMapping(value = "/getlist", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public List<AccountJson> getListAccount() {
		List<Account> listAccount = services.getListAccount();
		List<AccountJson> list = new ArrayList<AccountJson>();
		for (int i = 0; i < listAccount.size(); i++) {
			Account a = listAccount.get(i);
			AccountJson b = new AccountJson(a.getIdaccount(), a
					.getAccountType().getNametype(), a.getStatusaccount()
					.getName(), a.getCustomer().getIdcustomer(),
					a.getNameaccount(), a.getBalance());
			list.add(b);
		}

		return list;
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.POST, headers="Accept=application/json")
	@ResponseBody
	public AccountJson getAccount(@PathVariable("id") String id){
		Account a = services.getAccountById(id);
		AccountJson b = new AccountJson(a.getIdaccount(), a
				.getAccountType().getNametype(), a.getStatusaccount()
				.getName(), a.getCustomer().getIdcustomer(),
				a.getNameaccount(), a.getBalance());
		return b;
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST, headers="Accept=application/json")
	@ResponseBody
	public void updateAccount(@RequestBody AccountJson aj){
		System.out.println(aj.getIdaccount());
		String name = aj.getIdAccountType();
		int id = 0;
		if(name.equals("Saving Account")){
			id = 1;
		}
		if(name.equals("Deposit Account")){
			id = 2;
		}
		
		AccountType at = new AccountType(id, name);
		
		String namesta = aj.getIdStatusaccount();
		int idsta = 0;
		if(namesta.equals("new")){
			idsta = 1;
		}
		if(namesta.equals("active")){
			idsta = 2;
		}
		if(namesta.equals("block")){
			idsta = 3;
		}
		
		Statusaccount sta = new Statusaccount(idsta, namesta);
		
		Customer cus = services.getCustomerById(aj.getIdCustomer());
		
		Account ac = new Account(aj.getIdaccount(), at, sta, cus, aj.getNameaccount(), aj.getBalance());
		
		services.updateAccount(ac);
		
	}
}
